<template>
  <el-container class="bigbox">
    <el-header>
      <img src="../../image/study.jpg">
      <span>微服务卷B</span>
    </el-header>
    <el-container>
      <!--左侧-->
      <el-aside style="position: relative;"></el-aside>
      <!--右侧-->
      <el-main>
        <div class="fromBox">
          <div class="signup_div">
            <span class="signup-btn" :class="{'bottom' : bottom.active}" ref="signup_btn" @click="signupBtn">登录</span>
            <span class="line">|</span>
            <span class="signdown-btn" :class="{'bottom' :!bottom.active}" ref="signdown_btn" @click="signdownBtn">注册</span>
          </div>
          <el-form v-if="signupOrSigndown" ref="loginRefForm" :model="user" label-width="100px" :rules="loginRules">
            <el-form-item label="账号" prop="username">
              <el-input v-model="user.username" placeholder="4到16位(字母,数字,下划线,减号)" />
            </el-form-item>
            <el-form-item label="密码" prop="password">
              <el-input type="password" v-model="user.password" placeholder="6到18位数字" />
            </el-form-item>
            <el-form-item>
              <el-button class="signupBtn" type="primary" @click="login">登 录</el-button>
            </el-form-item>
          </el-form>
          <el-form v-else ref="loginRefForm" :model="userSignIn" class="signUp" label-width="100px" :rules="signInRules">
            <el-form-item label="用名：" prop="username">
              <el-input v-model="userSignIn.username" placeholder="4到16位(字母,数字,下划线,减号)" />
            </el-form-item>
            <el-form-item label="密码：" prop="password">
              <el-input v-model="userSignIn.password" type="password" placeholder="6到18位数字" />
            </el-form-item>
            <el-form-item label="确认密码：" prop="passwordAgain">
              <el-input v-model="userSignIn.passwordAgain" type="password" placeholder="请再次输入密码" />
            </el-form-item>
            <el-form-item>
              <el-button type="primary" class="signDownBtn" @click="signIn">注册</el-button>
            </el-form-item>
          </el-form>
        </div>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
import { doGet, doPost } from "../../http/httpUserRequest.js";
import { getTokenName, messageTip, removeToken } from "../../util/util.js";
import Vue from 'vue';

export default {
  data() {
    return {
      signupOrSigndown: true,
      bottom: { active: true },
      user: {},
      userSignIn: {
        username: '',
        password: '',
        passwordAgain: ''
      },
      name: '',
      loginRules: {
        username: [
          { required: true, message: '请输入登录账号', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入登录密码', trigger: 'blur' },
          { min: 6, max: 16, message: '登录密码长度为6-16位', trigger: 'blur' }
        ]
      },
      signInRules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 4, max: 16, message: '用户名长度应在4到16个字符之间', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, max: 18, message: '密码长度应在6到18个字符之间', trigger: 'blur' }
        ],
        passwordAgain: [
          { required: true, message: '请确认密码', trigger: 'blur' },
          { validator: this.checkPasswordAgain, trigger: 'blur' }
        ]
      }
    };
  },
  mounted() {
    this.freeLogin();
  },
  methods: {
    checkPasswordAgain(rule, value, callback) {
      if (value!== this.userSignIn.password) {
        callback(new Error('两次输入的密码不一致'));
      } else {
        callback();
      }
    },
    signupBtn() {
      this.bottom.active = true;
      this.signupOrSigndown = true;
    },
    signdownBtn() {
      this.bottom.active = false;
      this.signupOrSigndown = false;
    },
    signIn() {
      this.$refs.loginRefForm.validate((isValid) => {
        if (isValid) {
          let formData = new FormData();
          formData.append("username", this.userSignIn.username);
          formData.append("password", this.userSignIn.password);
          doPost("/user/signIn", formData).then((resp) => {
            if (resp.data.code === 200) {
              messageTip("登录成功", "success");
              removeToken();
              window.location.href = "/";
            } else {
              messageTip("登录失败", "error");
            }
          });
        }
      });
    },
    login() {
      this.$refs.loginRefForm.validate((isValid) => {
        if (isValid) {
          let formData = new FormData();
          formData.append("loginAct", this.user.loginAct);
          formData.append("loginPwd", this.user.loginPwd);
          formData.append("captcha", this.user.captcha);
          formData.append("rememberMe", this.user.rememberMe);
          doPost("/user/login", formData).then((resp) => {
            if (resp.data.code === 200) {
              messageTip("登录成功", "success");
              removeToken();
              if (this.user.rememberMe === true) {
                window.localStorage.setItem(getTokenName(), resp.data.data);
              } else {
                window.sessionStorage.setItem(getTokenName(), resp.data.data);
              }
              window.location.href = "/dashboard";
            } else {
              messageTip("登录失败", "error");
            }
          });
        }
      });
    },
    freeLogin() {
      let token = window.localStorage.getItem(getTokenName());
      if (token) {
        doGet("/user/login/free", {}).then(resp => {
          if (resp.data.code === 200) {
            window.location.href = "/dashboard";
          }
        });
      }
    }
  }
};
</script>

<style scoped>
.captchaImg {
  margin-left: 20px;
}
.signupBtn {
  width: 100px;
  margin-right: 20px;
}
.bigbox {
  height: calc(98vh);
  width: 100%;
}
.fromBox {
  border: 1px solid grey;
  box-shadow: 0px 0px 10px 1px grey;
  border-radius: 4px;
  width: 441px;
  height: 303px;
  margin: auto;
  margin-top: 20%;
}
.signup_div {
  margin-bottom: 30px;
  margin-top: 30px;
  margin-left: 50px;
}
.signup_div.line {
  font-size: 20px;
}
.signup_div.signdown-btn {
  font-size: 25px;
  font-weight: bold;
  cursor: pointer;
}
.signup_div.signdown-btn:hover {
  color: #393939;
}
.bottom {
  border-bottom: 6px solid skyblue;
}
.signup_div.signup-btn {
  font-size: 25px;
  font-weight: bold;
}
.signup_div.signup-btn:hover {
  color: #393939;
  cursor: pointer;
}
.left-image {
  width: 100%;
}
.el-input {
  width: 80%;
}
.el-aside {
  background: #1a1a1a;
  width: 50%;
  background: url(../../image/SignUp.jpg);
  background-repeat: no-repeat;
  background-size: cover;
}
.el-main {
  height: 100%;
}
.imgTitle {
  color: #f9f9f9;
  font-size: 28px;
}
.loginTile {
  text-align: center;
  margin-top: 100px;
  margin-bottom: 25px;
  font-weight: bold;
}
.el-header {
  width: 100%;
  height: 8%;
  background-color: #393939;
  position: relative;
}
.el-header span {
  font-weight: bold;
  font-size: 30px;
  font-style: italic;
  vertical-align: top;
  line-height: 250%;
}
.el-header img {
  width: 4%;
  height: 100%;
  margin-left: 2%;
}
</style>